<?php

require_once 'Gepard/classes/app.php';

require_once 'Gepard/classes/view/abstract.php';

require_once 'Gepard/classes/view/area.php';

require_once 'Gepard/classes/view/adapters/default.php';

require_once 'Gepard/classes/view/adapters/phtml.php';

/**
 * @package core/view
 * @desc 
 * @author taras
 *
 */
class gView extends gCoreObject
{
    /**
     * Global layout
     *
     * @var
     */
	protected $_layout = array('name'=>'','path'=>'','url'=>'');
    /**
     * Helpers store
     * @var <type>
     */
    protected $_helpers = array();
    /**
     * Adapter
     * @var gViewDefault
     */
    protected $_defaultAdapter = null;
    /**
     * Adapter for phtml files
     * @var gViewPhtml
     */
    protected $_phtmlAdapter = null;
    /**
     *
     * @var gViewAdapterAbstract
     */
    protected $_adapter = null;
    /**
     * 
     * Enter description here ...
     * @var gViewArea
     */
    public $_area = null;
    
    protected static  $_disabledOutput = false;
    
    protected $_styles = array();
    
    protected $_scripts = array();
    
    /**
     *
     */
    public function init()
    {
        $layout = gApp::get('layout');
		if( !is_array($layout))
		{
			$this->_layout['name'] = $layout;
		}
        
		$this->_area = new gViewArea($this->core);
				
        $this->_defaultAdapter = new gViewDefault( $this->core );

        $this->_phtmlAdapter = new gViewPhmtl( $this->core );
    }
    
    /**
     * 
     * Enter description here ...
     * @param unknown_type $name
     */
    
    public function setLayout($name = null)
    {
    	if( $name == null )
    	{
    		$name = gApp::get('layout');
    	}
    	$this->_layout['name'] = $name;
    }

    
    public function getLayoutName()
    {
    	return $this->_layout['name'];
    }

    public function switchAdapter()
    {
        
    }

    /**
     * 
     * @param string $name
     */
    public static function includeHelper($name)
    {
        $file = gApp::getAsPath('view_path') . DIRECTORY_SEPARATOR . 'helpers' . DIRECTORY_SEPARATOR . $name . '.php';
    }
    /**
     *
     * @param string $name
     */
    public function loadHelper($name)
    {
        
    }
	
	public function error($message)
	{
		$this->_area->error($message);
	}
	
	public function message($message)
	{
		$this->_area->message($message);
	}

	 public function template($name,$data = null)
	 {
	
	}

	public function parsetext($text,$data = null,$i18n = null)
	{
		
	}


    public function loadView()
    {
        
    }

    public function render()
    {
    	if( self::$_disabledOutput) return;
		$filename = gApp::getPath() . 'views' . DIRECTORY_SEPARATOR . $this->_layout['name'] . DIRECTORY_SEPARATOR . 'layout.phtml';
		require_once $filename;
    }
    
    public static function disableOutput()
    {
    	self::$_disabledOutput = true;
    	return;
    }
    
    public function renderScript($file = null,$vars = null)
    {
    	if( $file == null)
    		return false;
    	$filename = gApp::getPath() . 'views' . DIRECTORY_SEPARATOR . $this->_layout['name'] . DIRECTORY_SEPARATOR . $file;
    	if( file_exists($filename))
    	{
    		ob_start();
    		require_once $filename;
    		$data = ob_get_clean();
    		return $data;
    	}
    	else
    	{
    		return false;
    	}
    }
    
    public function area()
    {
    	return $this->_area;
    }
    
        
    /* Document */
    public function headStyle()
    {
    	
    }
    
    public function headScript($type=null,$data = null)
    {
    	if( $type==null)
    	{
    		$data = implode('\n',$this->_scripts);
    		return $data;
    	}
    	else
    	{
    		if( $type=='file')
    		{
    			$this->_scripts[] = '<script language="javascript" type="text/javascript" src="'.$data.'"></script>';
    		}
    	}
    	
    }
    
    
    
    
}